package com.cat.dynamicPlanning;

import javax.naming.CannotProceedException;
import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/maximum-score-of-spliced-array/description/
 * @create 2025/8/30 20:01
 * @since JDK17
 */

public class Solution28 {
    int n;

    int f(int[] nums1, int[] nums2, int sum) {
        int[] t = new int[n];
        for (int i = 0; i < n; i++) {
            t[i] = nums1[i] - nums2[i];
        }
        int last = t[0], cur, ans = t[0];
        for (int i = 1; i < n; i++) {
            cur = t[i] + Math.min(last, 0);
            last = cur;
            ans = Math.min(ans, cur);
        }

        return sum - ans;
    }

    public int maximumsSplicedArray(int[] nums1, int[] nums2) {
        n = nums1.length;
        int sum1 = Arrays.stream(nums1).sum();
        int sum2 = Arrays.stream(nums2).sum();
        return Math.max(f(nums1, nums2, sum1), f(nums2, nums1, sum2));
    }
}
